package com.chixing.day03.day03_Test;

public class UglyNumber {
    public static void main(String[] args) {
/*        do {
            if (y==2|y==3|y==5) {
                x=i;
                count++;
                break;
            } else if(y%2==0){
                y /= 2;
            } else if(y%3==0){
                y /= 3;
            } else if(y%5==0){
                y /= 5;
            } else{
                isUgly = false;
                break;
            }
        }while(y>=2);*/

        int count = 1;
        int uglyIndex = 18; // 找出第10个丑数
        for (int i = 2; i <= 100; i++) { // i =  14
            boolean isUglyNumber = true;
            if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
                for (int j = i - 1; j > 5; j--) {
                    boolean result = isPrime(j);
                    if (result && i % j == 0) {
                        // System.out.println("当前数字：" + i + "，不是丑数");
                        isUglyNumber = false;
                        break;
                    }
                }
            } else {
                isUglyNumber = false;
            }
            if (isUglyNumber) {
                System.out.println("当前数字：" + i + "是丑数");
                count++;
                if (count == uglyIndex) {
                    System.out.println("第" + uglyIndex + "个丑数是：" + i);
                    break;
                }
            }
        }
    }

    // 判断是不是素数
    public static boolean isPrime(int num) {
        for (int i = 2; i <= num - 1; i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}
